package com.cleargroup.magic.cleara.c

import com.aa.base.sp.BaseSp


/**
 * @author Afra55
 * @date 7/16/21
 * A smile is the best business card.
 */
object AppCacheSeeYouSp : BaseSp("app_cache_sy") {

    @JvmStatic
    fun setAppCacheFirstScanTime(packageName: String, force: Boolean = false) {
        if (!force && getAppCacheFirstScanTime(packageName) > 0) {
            // nothing
        } else {
            setValue("f_st_$packageName", System.currentTimeMillis())
        }
    }

    @JvmStatic
    fun getAppCacheFirstScanTime(packageName: String): Long {
        return getLongValue("f_st_$packageName", 0L)
    }

    @JvmStatic
    fun setAppCacheSize(packageName: String, type: Int, long: Long, reset: Boolean = false) {
        var any = 0L
        if (reset) {
            // nothing
        } else {
            try {
                any = getAppCacheSize(packageName, type) + long
                if (any > Long.MAX_VALUE / 2) {
                    any = Long.MAX_VALUE / 2
                }
            } catch (e: Exception) {
            }
            val maxAppCachePackageName = getMaxAppCachePackageName()
            if (maxAppCachePackageName.isNullOrEmpty()) {
                setMaxAppCachePackageName(packageName)
            } else {
                if (getAppCacheSize(packageName) > getAppCacheSize(maxAppCachePackageName)) {
                    setMaxAppCachePackageName(packageName)
                }
            }
        }
        setValue("acs$type$packageName", any)
    }

    @JvmStatic
    fun getAppCacheSize(packageName: String, type: Int): Long {
        return getLongValue("acs$type$packageName", 0)
    }

    @JvmStatic
    fun getAppCacheSize(packageName: String): Long {
        return getAppCacheSize(packageName, 0) + getAppCacheSize(packageName, 1)
    }

    @JvmStatic
    fun setMaxAppCachePackageName(packageName: String) {
        setValue("maxcache", packageName)
    }

    @JvmStatic
    fun getMaxAppCachePackageName(): String? {
        return getStringValue("maxcache")
    }

    @JvmStatic
    fun setLastCheckTime() {
        setValue("last_check_time", System.currentTimeMillis())
    }

    @JvmStatic
    fun getLastCheckTime(): Long {
        return getLongValue("last_check_time", 0)
    }
}